package com.easondongh.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easondongh.system.utils.StringUtils;
import com.easondongh.common.vo.BasePageQueryVo;
import com.easondongh.system.vo.LogQueryVo;
import com.easondongh.common.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easondongh.system.mapper.SysLogMapper;
import com.easondongh.system.domain.SysLog;
import com.easondongh.system.service.SysLogService;

/**
 * @author EasonDongH
 * @date 2020/4/30 11:08
 */
@Service
public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> implements SysLogService {

    @Autowired
    private SysLogMapper logMapper;

    @Override
    public PageResult<SysLog> getByPage(BasePageQueryVo<LogQueryVo> queryCondition) {
        IPage<SysLog> page = new Page<>(queryCondition.getPage(), queryCondition.getLimit());
        QueryWrapper<SysLog> queryWrapper = new QueryWrapper<>();
        LogQueryVo queryParams = queryCondition.getQueryParams();
        if(queryParams != null) {
            queryWrapper.eq(queryParams.getResponseStatus() != null,
                    SysLog.COL_RESPONSE_STATUS, queryParams.getResponseStatus());
            queryWrapper.like(StringUtils.isNotBlank(queryParams.getLoginName()),
                    SysLog.COL_LOGIN_NAME, queryParams.getLoginName());
            queryWrapper.like(StringUtils.isNotBlank(queryParams.getRequestUrl()),
                    SysLog.COL_REQUEST_URL, queryParams.getRequestUrl());
            queryWrapper.like(StringUtils.isNotBlank(queryParams.getResponseError()),
                    SysLog.COL_RESPONSE_ERROR, queryParams.getResponseError());
        }
        queryWrapper.orderByDesc(SysLog.COL_CREATE_TIME);
        IPage<SysLog> iPage = this.logMapper.selectPage(page, queryWrapper);
        return  new PageResult<>(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), iPage.getRecords());
    }

    @Override
    public Boolean deleteByIds(List<Integer> ids) {
        int i = this.logMapper.deleteBatchIds(ids);
        return i == ids.size();
    }
}

